package com.ujiuye.response;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.xpath.XPath;
import java.io.FileInputStream;
import java.io.IOException;
/*
响应体响应流资源
图片、视频..
D:\apache-tomcat-8.5.69\webapps\day12\img
*/
@WebServlet("/r5")
public class ResponseDemo4 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //通知浏览器返回的类型
        resp.setContentType("image/jpg");
        //获取a.jpg这个图片在服务器中真是路径
        String path = req.getServletContext().getRealPath("/img/1.jpg");
        //创建文件字节输入流对象
        FileInputStream  fis = new FileInputStream(path);
        byte[] arr = new byte[1024*8];
        int len;//读取到的有效字节个数
        //获取响应流
        ServletOutputStream sos = resp.getOutputStream();
        while ((len=fis.read(arr))!=-1){
            sos.write(arr,0,len);
        }
        sos.close();
        fis.close();
    }
}
